package me.aartikov.alligator.animations;


import me.aartikov.alligator.util.Log;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.transition.Transition;
import ohos.utils.Pair;

import java.util.ArrayList;
import java.util.List;

/**
 * Transition animation that uses activity and fragment transitions introduced in Lollipop (API 21).
 * Warning: some methods are not supported for activities (see method description).
 */
public class LollipopTransitionAnimation implements TransitionAnimation {
    private Transition mEnterTransition;
    private Transition mExitTransition;
    private Transition mSharedElementTransition;
    private boolean mAllowEnterTransitionOverlap = true;
    private List<Pair<Component, String>> mSharedElements;

    /**
     * Creates with default settings. Can be used for activities to enable Lollipop transitions (Window.FEATURE_ACTIVITY_TRANSITIONS should be also set for an activity).
     */
    public LollipopTransitionAnimation() {
    }

    /**
     * Creates with given enter and exit transitions. Warning: is not supported for activities.
     *
     * @param enterTransition transition that will be used for an appearing fragment
     * @param exitTransition  transition that will be used for a disappearing fragment
     */
    public LollipopTransitionAnimation(Transition enterTransition, Transition exitTransition) {
        mEnterTransition = enterTransition;
        mExitTransition = exitTransition;
    }


    public Transition getEnterTransition() {
        return mEnterTransition;
    }

    /**
     * Sets a transition that will be used for an appearing fragment. Warning: is not supported for activities, should be set manually in activity's onCreate method.
     *
     * @param enterTransition transition that will be used for an appearing fragment
     * @return this object
     */
    public LollipopTransitionAnimation setEnterTransition(Transition enterTransition) {
        mEnterTransition = enterTransition;
        return this;
    }


    public Transition getExitTransition() {
        return mExitTransition;
    }

    /**
     * Sets a transition that will be used for a disappearing fragment. Warning: is not supported for activities, should be set manually in activity's onCreate method.
     *
     * @param exitTransition transition that will be used for a disappearing fragment
     * @return this object
     */
    public LollipopTransitionAnimation setExitTransition(Transition exitTransition) {
        mExitTransition = exitTransition;
        return this;
    }


    public Transition getSharedElementTransition() {
        return mSharedElementTransition;
    }

    /**
     * Sets a transition that will be used for shared elements. Warning: is not supported for activities, should be set manually in activity's onCreate method.
     *
     * @param sharedElementTransition transition that will be used for shared elements
     * @return this object
     */
    public LollipopTransitionAnimation setSharedElementTransition(Transition sharedElementTransition) {
        mSharedElementTransition = sharedElementTransition;
        return this;
    }

    public boolean isAllowEnterTransitionOverlap() {
        return mAllowEnterTransitionOverlap;
    }

    /**
     * Sets whether the exit transition and the enter transition overlap or not. Warning: is not supported for activities, should be set manually in activity's onCreate method.
     *
     * @param allowEnterTransitionOverlap true to start the enter transition when possible or false to wait until the exiting transition completes
     * @return this object
     */
    public LollipopTransitionAnimation setAllowEnterTransitionOverlap(boolean allowEnterTransitionOverlap) {
        mAllowEnterTransitionOverlap = allowEnterTransitionOverlap;
        return this;
    }


    public List<Pair<Component, String>> getSharedElements() {
        return mSharedElements;
    }

    /**
     * Adds a shared element that will be used in a transition.
     *
     * @param sharedElement shared element view in a disappearing activity/fragment
     * @param name          shared element name in an appearing activity/fragment
     * @return this object
     */
    public LollipopTransitionAnimation addSharedElement(Component sharedElement, String name) {
        if (mSharedElements == null) {
            mSharedElements = new ArrayList<>();
        }
        mSharedElements.add(new Pair<>(sharedElement, name));
        return this;
    }

    @Override
    public int getEnterAnimal() {
        return 0;
    }

    @Override
    public int getExitAnimal() {
        return 0;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Operation getActivityOptionsBundle(Ability activity) {
        //TODO 目前未找到替代方式
        return activity.getIntent().getOperation();
//		if (mSharedElements == null) {
//			return ActivityOptionsCompat.makeSceneTransitionAnimation(activity).toBundle();
//		} else {
//			Pair<Component, String>[] sharedElements = mSharedElements.toArray(new Pair[mSharedElements.size()]);
//			return ActivityOptionsCompat.makeSceneTransitionAnimation(activity, sharedElements).toBundle();
//		}
    }

    @Override
    public boolean needDelayActivityFinish() {
        return true;
    }

    @Override
    public void applyBeforeActivityStarted(Ability currentActivity, Intent intent) {
    }

    @Override
    public void applyAfterActivityStarted(Ability currentActivity) {
    }

    @Override
    public void applyBeforeActivityFinished(Ability activity) {
    }

    @Override
    public void applyAfterActivityFinished(Ability activity) {
    }

    @Override
    public void applyBeforeFragmentTransactionExecuted(FractionScheduler transaction, Fraction enteringFragment, Fraction exitingFragment) {
        //TODO 需要设置跳转的动画逻辑 暂无对照
        Log.e("未完成", "需要设置跳转动画");


        //        enteringFragment.setEnterTransition(mEnterTransition);
//        exitingFragment.setExitTransition(mExitTransition);
//        enteringFragment.setSharedElementEnterTransition(mSharedElementTransition);
//        enteringFragment.setAllowEnterTransitionOverlap(mAllowEnterTransitionOverlap);
//        if (mSharedElements != null) {
//            for (Pair<View, String> sharedElement : mSharedElements) {
//                transaction.addSharedElement(sharedElement.first, sharedElement.second);
//            }
//        }
    }

    @Override
    public void applyAfterFragmentTransactionExecuted(Fraction enteringFragment, Fraction exitingFragment) {

        //TODO 需要设置跳转的动画逻辑 暂无对照
        Log.e("未完成", "清除数据");

//        enteringFragment.setEnterTransition(null);
//        exitingFragment.setExitTransition(null);
//        enteringFragment.setSharedElementEnterTransition(null);
//        enteringFragment.setAllowEnterTransitionOverlap(true);
    }
}
